package com.hxgis.oc.util.fdb;

import com.hxgis.oc.config.OConfig;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Component
public class FDBManager {
    private static final Map<String,String> fdbFileMap = new HashMap<>();
    private static final Map<String,FDB> fdbMap = new HashMap<>();
    private static boolean initialize = false;

    public static FDB getFDB(String name)
    {
        if(!initialize)
        {
            initialize = true;
            String[] fdb = OConfig.getConfig().getFdb();
            for (String fileName : fdb ) {
                File file = new File(fileName);
                if(file.exists())
                {
                    String simpleFileName = file.getName();
                    simpleFileName = simpleFileName.replaceAll(".fdb","");
                    fdbFileMap.put(simpleFileName,fileName);
                }
            }
        }

        FDB fdb = FDBManager.fdbMap.get(name);
        if( fdb == null)
        {
            String fileName = fdbFileMap.get(name);
            if( fileName != null)
            {
                fdb = new FDB(fileName);
                fdbMap.put(name,fdb);
                fdbFileMap.remove(name);
            }
        }

        return fdb;
    }
}
